#!/bin/bash
# - package_name
. $LKP_SRC/lib/debug.sh
. $LKP_SRC/lib/upload.sh
. $LKP_SRC/lib/log.sh

: "${DISTRO:=openeuler}"
: "${basearch:=$(arch)}"
. $LKP_SRC/distro/${DISTRO}

source $LKP_SRC/tests/compatibility-test.conf

validate_conf_file()
{
	install_command_nonblank=$(echo $install_command | awk '$1=$1')
	remove_command_nonblank=$(echo $remove_command | awk '$1=$1')
	start_service_nonblank=$(echo $start_service | awk '$1=$1')
	stop_service_nonblank=$(echo $stop_service | awk '$1=$1')
	if [[ -z "$install_command_nonblank" ]] || [[ -z "$remove_command_nonblank" ]] || [[ -z "$start_service_nonblank" ]] || [[ -z "$stop_service_nonblank" ]];then
		echo -e "---------THE COMMAND CAN NOT BE BLANK OR EMPTY AT COMPATIBILITY-TEST.CONF---------"
		exit 2
	fi
	return 0
}

package_name=($package_name)

os_info=$(cat /etc/os-release| grep PRETTY_NAME | awk -F '"' '{print $2}')

if [[ $os_info =~ "(" ]]; then
        os_info_1=$(echo $os_info | awk -F '(' '{print $1}' | tr ' ' '-')
        os_info_2=$(echo $os_info | awk -F '(' '{print $2}' | awk -F ')' '{print $1}')
        os_info_3="$os_info_1-$os_info_2"
else
        os_info_3=$(echo $os_info | tr ' ' '-')
fi

test_date=$(date "+%Y-%m-%d %H:%M:%S")
unique_id="$os_info_3-$(uname -i) $test_date"
echo "compatibility-tools-identification: $unique_id"

echo_package_location()
{       
        for i in "${package_name[@]}"
        do
                echo "Starting print package: ${i} location"
                repoquery --location $i 2>/dev/null | grep -v src.rpm
                echo "print package: ${i} location end"
        done
}

echo_package_evr()
{
	for i in "${package_name[@]}"
        do
                echo "Starting print package: ${i} evr"
                repoquery --queryformat '%{evr}' $i 2>/dev/null
                echo "print package: ${i} evr end"
        done

}

echo_src_package_location()
{
	for i in "${package_name[@]}"
        do
                echo "Starting print package: ${i}'s src.package location"
                src_package_name=$(repoquery --queryformat '%{source_name}' $i 2>/dev/null)

		[[ -n "$src_package_name" ]] && {
			repoquery --location $src_package_name 2>/dev/null | grep src.package
		}
                echo "print package: ${i}'s src.package location end"
        done
}

echo_package_license()
{
	for i in "${package_name[@]}"
        do
                echo "Starting print package: ${i} license"
                repoquery --qf '%{license}' $i 2>/dev/null
                echo "print package: ${i} license end"
        done
}

install_packages()
{
	local installed_packages=()
	log_info "Starting install test: ${package_name[@]}"
	for i in "${package_name[@]}"
	do
		if [[ $i =~ '.rpm' ]]; then
			rpm -ivh $i
			if [[ $? -eq 0 ]]; then
				echo "package install result, package name: $i, state: pass"
				local index=${#installed_packages[@]}
				installed_packages[$index]=$i
			else
				echo "package install result, package name: $i, state: fail"
			fi
		else
			cp /home/jar/hello.jar /home/hello.jar
			$install_command
			if [[ $? -eq 0 ]]; then
				local index=${#installed_packages[@]}
                                installed_packages[$index]=$i
				echo "package install result, package name: $i, state: pass"
			else
				echo "package install result, package name: $i, state: fail"
			fi
		fi
		break
	done

	package_name=("${installed_packages[@]}")
}

query_group()
{	
	for i in "${package_name[@]}"
	do
		local group=$(repoquery --installed --qf '%{group}' $i | head -n 2 | tail -n 1)
		echo "query group result, package name: $i, group: $group"

	done
}

query_libs()
{
	for i in "${package_name[@]}"
	do
		repoquery -l $i | grep -E "\.so$|\.a$"
		if [[ $? -eq 0 ]]; then
			echo "query libs result, package name: $i, libs: true"
		else
			echo "query libs result, package name: $i, libs: false"
		fi
	done
}

run_cmd()
{
	log_info "Starting run cmd"
	for i in "${package_name[@]}"
	do
		local cmd_list=($(rpm -ql $i | grep -E '/s?bin/'))
                [[ -z "${cmd_list}" ]] || {
                        for j in "${cmd_list[@]}"
                        do
				strings $j | grep -- --help >/dev/null 2>&1
				if [[ $? -eq 0 ]]; then
					timeout 5 $j --help >/dev/null 2>&1
					if [[ $? -eq 0 ]]; then
						echo "run cmd help result, package name: $i, cmd: $j, state: pass"
					else
						echo "run cmd help result, package name: $i, cmd: $j, state: fail"
					fi
				fi
                        done
                }
        done
}


run_service()
{
	log_info "Starting run service start/status/stop"
        for i in "${package_name[@]}"
	do
                if [[ $i =~ '.rpm' ]]; then
                        local service_list=($(rpm -ql $i | grep -E "\.service$" | awk -F "/" '{print $NF}'))
                        [[ -z "${service_list}" ]] || {
                                for j in "${service_list[@]}"
                                do
                                        for k in "start" "status" "stop"
                                        do
                                                timeout 10 systemctl ${k} ${j} >/dev/null 2>&1
                                                if [[ $? -ne 0 ]]; then
                                                        echo "run service action result, package name: $i, service: $j, action: $k, state: fail"
                                                else
                                                        echo "run service action result, package name: $i, service: $j, action: $k, state: pass"
                                                fi
                                        done
                                done
                        }
                else
			timeout 80 $start_service
			if [[ $? -eq 0 ]]; then
				echo "run service action result, package name: $i, command: $start_service, action: start, state: pass"
		        else
				echo "run service action result, package name: $i, command: $start_service, action: start, state: fail"
			fi
			$stop_service
            		if [[ $? -eq 0 ]]; then
				echo "run service action result, package name: $i, command: $stop_service, action: stop, state: pass"
			else
				echo "run service action result, package name: $i, command: $stop_service, action: stop, state: fail"
			fi
		fi	
		break
        done
}


ldd_test()
{
	log_info "Starting ldd test"
	for i in "${package_name[@]}"
	do
		local ldd_list=$(rpm -ql $i | awk -F'/' '$3 ~ /bin|sbin|lib|lib64/')
		echo ${ldd_list}
		[[ -z "${ldd_list}" ]] || {
			for i in "${ldd_list[@]}"
			do
				[[ -f "$i" && -x "$i" ]] && {
					echo "ldd for $i"
					ldd -d -r $i

				}
			done
		}
	done
}


nmap_test(){
	log_info "Starting nmap test tcp"
	local_ip=$(hostname -I | awk '{print $1}')
	nmap -sS -A -v --reason -p- -n -Pn ${local_ip}
	log_info "Starting nmap test udp"
	nmap -sU -A -v --reason -p- -n -Pn -T4  ${local_ip}
	log_info "Starting nmap test protocol"
	nmap -sO -v --reason -n  ${local_ip}
}

clamav_test()
{
	log_info "Starting clamav test"
	yum install clamav -y
	if [[ $? -ne 0 ]]; then
		echo "clamav test fail"
		return 
	fi
	for i in "${package_name[@]}"
        do
		clamscan -ri  "$i" 2>/dev/null | grep -v "LibClamAV Warning"
        done
}

remove_packages()
{
	log_info "starting remove test"
	for i in "${package_name[@]}"
	do
		if [[ $i =~ '.rpm' ]]; then
		        #local package_full_name=$(repoquery --installed --queryformat '%{name}-%{evr}.%{arch}' $i 2>/dev/null)

			rpm_name=$(echo $i | awk -F '/' '{print $NF}' | awk -F '.rpm' '{print $1}')
			rpm -e $rpm_name
			if [[ $? -eq 0 ]]; then
				echo "package uninstall result, package name: $i, state: pass"
			else
				echo "package uninstall result, package name: $i, state: fail"
			fi
		else
			$remove_command
			if [[ $? -eq 0 ]]; then
				echo "package uninstall result, package name: $i, command: $remove_command, state: pass"
			else
				echo "package uninstall result, package name: $i, command: $remove_command, state: fail"
			fi
		fi
		break
	done
}

validate_conf_file
distro_install_depends compatibility-test
echo_package_location
echo_package_evr
echo_src_package_location
echo_package_license
install_packages
query_group
query_libs
run_cmd
run_service
ldd_test
nmap_test
clamav_test
remove_packages

